﻿$(function () {

    var _url = "/ajaxdata/leader/";
    var id = $("input[name=rcid]").val();

    KindEditor.ready(function (K) {
        KE = K.create('#content', {
            resizeType: 1,
            allowPreviewEmoticons: false,
            allowImageUpload: false,
            items: [
				'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
				 'strikethrough', 'table', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
				'insertunorderedlist', '|', 'image', 'link', 'source']
        });

        if (id != "") {
            $.ajax({
                url: _url,
                data: { action: "clb_get", id: id },
                dataType: "json",
                success: function (data) {
                    var mtfild = data.Name.split('^');
                    $("input[name=clb_name]").val(mtfild[0]);
                    $(".thumimg").attr("src", data.Img);
                    $("input[name=vist_date]").val(mtfild[1]);
                    $("textarea[name=profile]").val(data.Profile);
                    KE.html(decodeURIComponent(data.Detail==null?"":data.Detail));
                }

            });
        }

    });

    $("#save").click(function () {
        var clb_date = $.trim($("input[name=clb_name]").val());
        if (clb_date == "") {
            alert("请输入名人名称。")
            return false;
        }
        var vist_date = $.trim($("input[name=vist_date]").val());
        if (vist_date == "") {
            alert("请输入专访时间。")
            return false;
        }
     
        var profile = $.trim($("textarea[name=profile]").val());
        if (profile == "") {
            alert("请输入名人简介。")
            return false;
        }

        var thumimg = $(".thumimg").attr("src");
        if (thumimg == "#" || thumimg.indexOf("no_photo.gif") > 0) {
            alert("请选名人封面。")
            return false;
        }

        var postdata = $(".webform").serializeArray();
        postdata.push({ name: "previewImg", value: $(".thumimg").attr("src") });
        postdata.push({ name: "content", value: encodeURIComponent(KE.html()) });
        postdata.push({ name: "action", value: "clb_save" });
        $.ajax({
            url: _url,
            type: "POST",
            data: postdata,
            success: function (data, state) {
                location = "list.aspx";
            },
            error: function (req, state, err) {
                alert("提交出错。");
            },
            beforeSend: function () {
                $("#save,.loading2").toggle();
            },
            complete: function () {
                $("#save,.loading2").toggle();
            }
        });
    });

    $(".selimg").click(function () {
        var imgurl = window.prompt("请输入名人封面地址。");
        if (imgurl != null) {
            $(".thumimg").attr("src", imgurl);
        }
    });


    

})